package com.taiic.deepseek;

import com.taiic.deepseek.rkllm.LLMCallState;
import com.taiic.deepseek.rkllm.RKLLMResult;
import com.taiic.deepseek.rkllm.onRkllmResult;

public class DsManager {
    private onRkllmResult onRkllmResult = null;

    // Used to load the 'deepseek' library on application startup.
    static {
        System.loadLibrary("deepseek");
    }
//    1. 静态内部类，只有在调用 GetInstance 时才会加载
    private static class DsManagerHolder{
        private static final DsManager INSTANCE = new DsManager();
    }
//    2. 私有的构造方法，防止外部直接实例化
    private DsManager(){
        //        可以初始化一些资源
    }
//    3. 公共的静态方法，用于获取唯一实例,线程安全的单例模式,静态内部类实现
    public static DsManager GetInstance()
    {
        return DsManagerHolder.INSTANCE;
    }

/* api methon */
    public boolean initializeModel(String modelPath, int maxNewTokens, int maxContextLen, onRkllmResult cb)
    {
        if(onRkllmResult == null)
            onRkllmResult = cb;

        boolean ret = initialize(modelPath, maxNewTokens, maxContextLen);
        return ret;
    }
    public void destoryModel(){

        if(onRkllmResult != null) {
            onRkllmResult = null;
        }
        destroy();
    }
    public void runModel(String input){
        run(input);
    }
    public boolean initializedModel()
    {
        return initialized();
    }


//    native方法，使能 运行 销毁模型：java->jni
    private native boolean initialize(String modelPath, int maxNewTokens, int maxContextLen);
    private native void run(String input);
    private native void destroy();
    private native boolean initialized();


//    本地方法，将rkllmResult传回java: jni->java
    public void onGetResult(RKLLMResult result, Object userdata, LLMCallState state){
        if(onRkllmResult != null)
            onRkllmResult.rkllmResult(result, userdata, state);
    }
    public void onGetResultSimple(String test, int state){
        if(onRkllmResult != null)
            onRkllmResult.rkllmResultSimple(test, state);
    }
    public void onFinsh(){
        if(onRkllmResult != null)
            onRkllmResult.rkllmFinish();
    }

}